package com.company.batch;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
public class InvokerJob {

	@Autowired
	private JobLauncher jobLauncher;

	@Autowired
	private ApplicationContext context;

	public String executeJob(String realFileName, String jobName)
			throws JobExecutionAlreadyRunningException, JobRestartException,
			JobInstanceAlreadyCompleteException, JobParametersInvalidException {
		Job job = (Job) context.getBean("jobName");
		JobParameters parameters = new JobParametersBuilder().addString(
				"realFileName", realFileName).toJobParameters();
		JobExecution execution = jobLauncher.run(job, parameters);
		return execution.getStatus().toString();
	}

}
